<?php
namespace app\index\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\View;
class Login extends First{

	//登录
	public function login(){
		if(session('?uid')){
			if (session('utype')==1) {
				return redirect('/index/pingfen.html');
			}else{
				return redirect('/index/student.html');
			}
		}else{
			if (request()->isPost()) {
				$type=input('type');//return json(input());
				if ($type==1) {
					$where['name']=input('name');
					$where['password']=md5(md5(input('password')));
					$where['type']=2;
					$res=Db::table('admin')->where($where)->find();$mes['msg']='账号或密码错误';
				}elseif ($type==2) {
					$where['name']=input('name');
					$where['idCard']=input('idCard');
					$res=Db::table('student')->where($where)->find();$mes['msg']='未找到相关考生信息';
				}elseif ($type==3) {
					$where['name']=input('name');
					$where['password']=md5(md5(input('password')));
					//$where['type']=3;
					$res=Db::table('admin')->where($where)->where('type=1 || type=3')->find();
					$mes['msg']='账号或密码错误';
				}else{
					$mes['code']=400;
					$mes['msg']='参数错误';
					return json($mes);
				}
				if ($res) {
					session('uid',$res['id']);
					session('utype',$res['type']);//halt(session('uid'));
					$mes['code']=200;
					$mes['msg']='登录成功';
				}else{
					$mes['code']=400;
				}
				return json($mes);
			}else{
				//dump(session('uid'));
				return view();
			}
		}
	}

	//机构申请
	public function jigou(){
		if (request()->isPost()) {
			if (input('name')!='' && input('contact')!='' && input('tel')!='') {
				$info=Db::table('apply')->where('province="'.input('province').'" and city="'.input('city').'" and area="'.input('area').'" and (name="'.input('name').'" or contact="'.input('contact').'" or tel="'.input('tel').'")')->find();
				//dump($info);die;
				if ($info) {
					$mes['code']=400;
					$mes['msg']='机构申请已提交，请不要重复提交！';
				}else{
					$data['province']=input('province');
					$data['city']=input('city');
					$data['area']=input('area');
					$data['name']=input('name');
					$data['address']=input('address');
					$data['contact']=input('contact');
					$data['tel']=input('tel');
					$data['time']=time();
					$res=Db::table('apply')->insert($data);
					if ($res) {
						$mes['code']=200;
						$mes['msg']='机构申请已提交，请等待回复！';
					}else{
						$mes['code']=400;
						$mes['msg']='申请提交出错，请重试!';
					}
				}
			}else{
				$mes['code']=400;
				$mes['msg']='请填写完整信息!';
			}

			return json($mes);
		}
	}

	//获取验证码
	function get_code(){
		if (request()->isPost()) {
			$mobile = input('phone');
			//return json(input());
			if (input('type')!='') {
				if (input('type')==1) {
					$num=Db::table('user')->where('mobile',$mobile)->find();
					if ($num) {
						$mes['state']=400;
						$mes['msg']='该账号已被注册';
						return json($mes);
					}
				}elseif(input('type')==2){
					$num=Db::table('user')->where('mobile',$mobile)->find();
					if (!$num) {
						$mes['state']=400;
						$mes['msg']='该账号已被注册';
						return json($mes);
					}
				}
				if($mobile!='' && preg_match("/^1[34578]\d{9}$/", $mobile)){
					$sms=controller('Smscode');
					$templateid = "329530";    //可在后台短信产品→选择接入的应用→短信模板-模板ID，查看该模板ID
					$param = rand(1000,9999); //多个参数使用英文逗号隔开（如：param=“a,b,c”），如为参数则留空
					$uid = "";
					//70字内（含70字）计一条，超过70字，按67字/条计费，超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
					$res = $sms->code($templateid,$param,$mobile,$uid);
					$res=json_decode($res,true);
					//return gettype($res);
					if ($res['code']==000000) {
						session('mobile',$mobile);
						session('smsCode',$param);
						$msg['state']=200;
						$msg['msg']='短信发送成功，请注意查收';
					}else{
						$msg['state']=400;
						$msg['msg']='短信发送失败';
					}
				}else{
					$msg['state']=400;
					$msg['msg']='手机号格式错误！';
				}
			}else{
				$msg['state']=400;
				$msg['msg']='参数错误';
			}
			return json($msg);
		}
	}

	//退出登录
	public function loginout(){
		session('utype',null);
		session('uid',null);
		if (session('?url')) {
			Header("Location: ".session('url'));exit;
		}else{
			$this->redirect('/');
		}
	}

	//学生成绩查询
	public function student(){
		$system=Db::table('system')->find(1);
		//halt($system);
		if(input('name')!='' || input('idcard')!=''){
			if(input('name')!='' && input('idcard')!=''){
				$where['name']=input('name');
				$where['idcard']=input('idcard');
			}
			if(input('name')!='' && input('idcard')==''){
				$where['name']=input('name');
			}
			if(input('name')=='' && input('idcard')!=''){
				$where['idcard']=input('idcard');
			}
			$list=Db::table('student')->where($where)->select()->toArray();
			//$list=db('student')->where('id',session('uid'))->select();
			$time=time()-7*24*60;
			foreach($list as $k=>$v){
				$list[$k]['cards']=Db::table('student_card')
					->alias('sc')
					->leftjoin('card c','sc.cid=c.id')
					->where('sc.sid='.$v['id'].' && sc.score_time<'.$time)
					->field('sc.score,c.name,sc.score_time,sc.time')
					->order('sc.time desc')
					->select()->toArray();
			}
			return view('./index/student',['list'=>$list]);
		}else{
			die('拒绝访问！');
		}
	}
}